package com.example.apiproject.service.impl;

import com.example.apiproject.entity.Friends;
import com.example.apiproject.dao.FriendsDao;
import com.example.apiproject.service.FriendsService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Friends)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("friendsService")
public class FriendsServiceImpl implements FriendsService {
    @Resource
    private FriendsDao friendsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Friends queryById(Integer id) {
        return this.friendsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param friends 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Friends> queryByPage(Friends friends, PageRequest pageRequest) {
        long total = this.friendsDao.count(friends);
        return new PageImpl<>(this.friendsDao.queryAllByLimit(friends, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param friends 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Friends friends) {
        int number=friendsDao.insert(friends);
        return number;
    }

    /**
     * 修改数据
     *
     * @param friends 实例对象
     * @return 实例对象
     */
    @Override
    public Friends update(Friends friends) {
        this.friendsDao.update(friends);
        return this.queryById(friends.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.friendsDao.deleteById(id) > 0;
    }

    @Override
    public List<Friends> queryAllByUserId(String id) {
        return this.friendsDao.queryAllByUserId(id);
    }

    @Override
    public int updatetobeFriend(int userId, int friendId, boolean agree) {
        if(agree) {
            int status=1;
            int result = friendsDao.updatetobeFriend(userId,friendId,status);
            Friends friends=new Friends();
            friends.setUserId(friendId);
            friends.setFriendId(userId);
            friends.setStatus(1);
            friends.setSource(1);
            friends.setCreateTime(new Date());

            int num=this.insert(friends);
            if(num==0) return  0;
            return result;
        }
        if(!agree) {
            int status=2;
            int result = friendsDao.updatetobeFriend(userId,friendId,status);
            return result;
        }
        return 0;
    }

    @Override
    public int ban(int userId, int friendId) {
        int i = friendsDao.banFriend(userId, friendId);
        if(i==0){
            i = friendsDao.addBanStranger(userId, friendId);
        }
        return i;
    }

    @Override
    public int cancelBan(int userId, int friendId) {
        Friends friend = friendsDao.queryIsFriends(userId,friendId);
        int i = 0;
        if (friend.getStatus()==3){
            i = friendsDao.cancelBan(userId, friendId);
        }else {
            i = friendsDao.deleteById(friend.getId());
        }
        return i;
    }

    @Override
    public Friends queryFriend(int userId, int friendId) {
        return friendsDao.queryIsFriends(userId, friendId);
    }
}
